// pages/user_center/travelagency/bill/billdetail.js
var util = require('../../../../utils/util.js');
var Tool = require("../../../../utils/ztool.js");
var tool = new Tool();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // sortway: '按公司',
    // sortwaytype: 1,
    usertype: '',
    dateStart: '',
    dateStarttemp: '',
    dateEnd: '',
    dateEndtemp: '',
    Totype: '',
    allCompBill: [],
    comname: '',
    stel: '',
    sjname: '',
    totalorderNum: 0,
    totalorderMoney: 0,
    month: '',
    mlmodelType: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {


    var useinfo = wx.getStorageSync('userInfo');
    this.data.usertype = useinfo.USerType
    if (this.data.usertype == 1 || this.data.usertype == 2) {
      if (options.type == 2) {
        this.setData({
          dateStart: options.datestart,
          dateEnd: options.dateend,
          Totype: options.type,
          comname: options.comname,
          dateStarttemp: options.datestart,
          dateEndtemp: options.dateend,
        })
      }
      if (options.type == 3 || options.type == 4) {
        this.setData({
          dateStart: options.datestart,
          dateEnd: options.dateend,
          Totype: options.type,
          stel: options.stel,
          dateStarttemp: options.datestart,
          dateEndtemp: options.dateend,
          sjname: options.sjname,
        })
      }
      

      this.chooseInterface()
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.chooseInterface()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  changeselect: function () {
    if (this.data.isselect) {
      this.setData({
        isselect: false,
      })
    } else {
      this.setData({
        isselect: true,
      })
    }
  },

  bindStartDateChange: function (e) {
    console.log(e)
    this.setData({
      dateStart: e.detail.value,
    })
  },
  bindEndDateChange: function (e) {
    console.log(e)
    this.setData({
      dateEnd: e.detail.value,
    })
  },

  search: function () {
    this.setData({
      isselect: false,
    })
    this.chooseInterface()
  },
  cleandate: function () {
    this.setData({
      dateStart: this.data.dateStarttemp,
      dateEnd: this.data.dateEndtemp,
    })
  },



  chooseInterface: function () {
    var that = this
    var url = ''
    var act = ''
    console.log(this.data.usertype)
    switch (this.data.usertype) {
      case '1':
        //对用车方
        if (this.data.Totype == 2) {
          url = 'https://www.lanshuicar.com/Server/ComputeTotal/CgsComTotalHdl.ashx'
          act = 'getMonthOrders'
          that.setData({
            mlmodelType: 12
          })
          this.getBillTotal(url, act)
        }
        //对司机
        if (this.data.Totype == 3) {
          url = 'https://www.lanshuicar.com/Server/ComputeTotal/CgsSjTotalHdl.ashx'
          act = 'getSingleDrOrders'
          that.setData({
            mlmodelType: 13
          })
          this.getBillTotal2(url, act)
        }

        break
      case '2':
        //对车公司
        if (this.data.Totype == 2) {
          url = 'https://www.lanshuicar.com/Server/ComputeTotal/LxsComTotalHdl.ashx'
          act = 'getMonthOrders'
          that.setData({
            mlmodelType: 12
          })
          this.getBillTotal(url, act)
        }
        //对司机
        if (this.data.Totype == 3) {
          url = 'https://www.lanshuicar.com/Server/ComputeTotal/LxsSjTotalHdl.ashx'
          act = 'getSingleDrOrders'
          that.setData({
            mlmodelType: 13
          })
          this.getBillTotal2(url, act)
        }
        if (this.data.Totype == 4) {
          url = 'https://www.lanshuicar.com/Server/ComputeTotal/LxsDyTotalHdl.ashx'
          act = 'getSingleGdOrders'
          that.setData({
            mlmodelType: 13
          })
          this.getBillTotal2(url, act)
        }
        break
    }

  },
  //获取订单
  getBillTotal: function (url, act) {
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 10000
    })
    var that = this
    var useinfo = wx.getStorageSync('userInfo');
    var pwd = wx.getStorageSync('password');
    var tel = ''
    if (that.data.usertype == '1' || that.data.usertype == '2') {
      tel = useinfo.Telphone
    }

    wx.request({
      url: url,
      data: {
        tel: tel,
        pwd: pwd,
        dates: that.data.dateStart,
        datee: that.data.dateEnd,
        act: act,
        comname: that.data.comname
      },
      method: 'GET',
      success: function (res) {
        wx.hideToast()
        if (res.statusCode == 404) {
          //接口加载失败
          console.log('加载接口错误');
          //显示错误信息
          that.showError('订单信息接口请求失败')
          that.setData({
            loadError: true
          });
          return
        }

        var data = tool.getRegularJson(res.data);
        console.log(data)
        if (data.code != 0) {
          that.showError(data.msg)
          return
        }
        if (data.data.length >= 0) {
          that.setData({
            allCompBill: data.data
          })
          that.dealdata1()


          console.log(that.data.allCompBill.length)
          //停止下拉刷新动画
          wx.stopPullDownRefresh();
          console.log(that.data.allCompBill)

        }
      },
      fail: function () {
        wx.hideToast()
        console.log('加载错误');
        that.setData({
          loadError: true
        });
      }
    })


  },

  //获取订单
  getBillTotal2: function (url, act) {
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 10000
    })
    var that = this
    var useinfo = wx.getStorageSync('userInfo');
    var pwd = wx.getStorageSync('password');
    var tel = ''
    if (that.data.usertype == '1' || that.data.usertype == '2') {
      tel = useinfo.Telphone
    }

    wx.request({
      url: url,
      data: {
        tel: tel,
        pwd: pwd,
        dates: that.data.dateStart,
        datee: that.data.dateEnd,
        act: act,
        stel: that.data.stel
      },
      method: 'GET',
      success: function (res) {
        wx.hideToast()
        if (res.statusCode == 404) {
          //接口加载失败
          console.log('加载接口错误');
          //显示错误信息
          that.showError('订单信息接口请求失败')
          that.setData({
            loadError: true
          });
          return
        }

        var data = tool.getRegularJson(res.data);
        console.log(data)
        if (data.code != 0) {
          that.showError(data.msg)
          return
        }
        if (data.data.length >= 0) {
          that.setData({
            allCompBill: data.data
          })
          that.dealdata1()


          console.log(that.data.allCompBill.length)
          //停止下拉刷新动画
          wx.stopPullDownRefresh();
          console.log(that.data.allCompBill)

        }
      },
      fail: function () {
        wx.hideToast()
        console.log('加载错误');
        that.setData({
          loadError: true
        });
      }
    })


  },


  dealdata1: function () {
    var that = this
    that.setData({
      totalorderNum: that.data.allCompBill.length
    })
    var data = that.data.allCompBill
    var totalmoney = 0
    for (var i = 0; i < data.length; i++) {
      var mony = parseInt(data[i].tMony)
      totalmoney = totalmoney + mony
    }
    var month = that.data.dateStart.slice(5, 7)
    month = getChinesemonth(month)
    that.setData({
      totalorderMoney: totalmoney,
      month: month
    })

  },




  // changeSortWay: function () {
  //   if (this.data.sortwaytype == 1) {
  //     this.setData({
  //       sortwaytype: 2,
  //       sortway: '按日期',
  //     })
  //   }else{
  //     this.setData({
  //       sortwaytype: 1,
  //       sortway: '按公司',
  //     })
  //   }
  // }

  showError: function (msg) {
    var that = this;
    that.setData({
      formError: true,
      errorMessage: msg
    });

    setTimeout(function () {
      that.setData({
        formError: false,
        errorMessage: '未知错误'
      });
    }, 2000);
  },
})

function getChinesemonth(month) {
  var chinese = ''
  switch (month) {
    case '01':
      chinese = '一'
      break
    case '02':
      chinese = '二'
      break
    case '03':
      chinese = '三'
      break
    case '04':
      chinese = '四'
      break
    case '05':
      chinese = '五'
      break
    case '06':
      chinese = '六'
      break
    case '07':
      chinese = '七'
      break
    case '08':
      chinese = '八'
      break
    case '09':
      chinese = '九'
      break
    case '10':
      chinese = '十'
      break
    case '11':
      chinese = '十一'
      break
    case '12':
      chinese = '十二'
      break

  }
  return chinese;
}